package com.frx.datastruct.chapterseven;

import java.util.ArrayList;

/**
 * Created by xurenfeng on 2019/3/5.
 */
public class BSTMap<K extends Comparable<K>,V > implements Map<K,V>{

    private class Node{

        private K key;
        private V val;
        private Node left;
        private Node right;

        public Node(K key,V val){
            this.key = key;
            this.val = val;
            this.left = null;
            this.right = null;
        }
    }

    private Node root;
    private int size;
    public BSTMap(){
        root = null;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void add(K key, V val) {
        root = add(key,val,root);
    }

    private Node add(K key,V val,Node node){

        if (node == null){
            size ++;
            return new Node(key,val);
        }

        if (node.key.compareTo(key) > 0){
            node.left = add(key, val, node.left);
        } else if (node.key.compareTo(key) < 0){
            node.right = add(key, val, node.right);
        } else {
            // 存在重复键的话更新键值
            node.val = val;
        }
        return node;
    }
    /** 返回以node为根节点的二分搜索树中，key所在的节点*/
    private Node getNode(Node node,K key){

        if (node == null){
            return null;
        }
        if (node.key.compareTo(key) > 0){
            return getNode(node.left,key);
        } else if (node.key.compareTo(key) < 0){
            return getNode(node.right,key);
        } else {
            return node;
        }
    }


    @Override
    public boolean contains(K key) {
        Node node = getNode(root,key);
        return node != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(root,key);
        return node == null ? null : node.val;
    }

    @Override
    public void set(K key, V val) {
        Node node = getNode(root,key);
        if (node == null){
            throw new IllegalArgumentException(key + " doesn't exist !");
        }
        node.val = val;

    }
    /** 返回以node为根节点的二分搜索树中最小值所在的节点*/
    private Node minmum(Node node){
        if (node.left == null){
            return node;
        }
        return minmum(node.left);
    }

    /** 删除以node为根节点的二分搜索树中最小值所在节点*/
    private Node removeMin(Node node){
        if (node.left == null){
            Node rightNode = node.right;
            node.right = null;
            size --;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    @Override
    public V remove(K key) {
        Node node = getNode(root,key);
        if (node != null){
            root = remove(root,key);
            return node.val;
        }
        return null;
    }

    private Node remove(Node node,K key){

        if (node == null){
            return null;
        }
        if (key.compareTo(node.key) < 0){
            node.left = remove(node.left,key);
            return node;
        } else if (key.compareTo(node.key) > 0){
            node.right = remove(node.right,key);
            return node;
        } else {
            if (node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            } else if (node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }
                // 左右都不为空的情况下删除节点
                // 首先寻找右边最小的元素
                Node successor = minmum(node.right);
                successor.right = removeMin(node.right);
                successor.left = node.left;
                node.left = node.right = null;
                return successor;
        }

    }

    public static void main(String[] args){
        final String FILE_PATH = "D:\\MyProject\\JavaDataStruct\\src\\com\\frx\\pride-and-prejudice.txt";
        ArrayList<String> words = new ArrayList<>();
        System.out.println("pride and prejudice");
        if (FileOperation.readFile(FILE_PATH,words)){
            System.out.println(" total words: " + words.size());
            BSTMap<String,Integer> bstMap = new BSTMap<>();
            for (String word : words){
                if (bstMap.contains(word)){
                    bstMap.set(word,bstMap.get(word) + 1);
                } else {
                    bstMap.add(word,1);
                }
            }
            System.out.println(" different words: " + bstMap.getSize());

            System.out.println(" frequency of pride: " + bstMap.get("pride"));
            System.out.println(" frequency of prejudice: " + bstMap.get("prejudice"));
        }

    }
}
